package com.mask.sms.controller;


import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.code.ThirdEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.util.RandomUtil;
import com.mask.roll.common.util.RollResponse;
import com.mask.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author hx
 */
@RestController
@RequestMapping("/sms")
@CrossOrigin
@Api(tags = "短信模块")
public class SmsController {

    @Autowired
    private SmsService smsService;


    /**
     * 短信发送
     * @param phone
     * @return
     */
    @GetMapping("/send/{phone}")
    @ApiOperation("短信发送")
    public RollResponse send(@PathVariable("phone") String phone) {
        if (StringUtils.isEmpty(phone)) {
            return RollResponse.result(ServiceEnums.SMS_PHONE_NULL.getCode(),
                    RollConstants.ERROR,ServiceEnums.SMS_PHONE_NULL.getMessage());
        }
        if (!phone.matches(RollConstants.SMS_REGEX_PHONE)) {
            return RollResponse.result(ServiceEnums.SMS_REGEX_PHONE.getCode(),
                    RollConstants.ERROR,ServiceEnums.SMS_REGEX_PHONE.getMessage());
        }
        return smsService.send(phone);

    }

    @PostMapping("/check/code")
    @ApiOperation("校验验证码")
    public RollResponse checkCode(
            @ApiParam("校验手机号") @RequestParam("phone") String phone,
            @ApiParam("校验验证码") @RequestParam("code") String code
            ){
        if (StringUtils.isEmpty(phone)) {
            return RollResponse.result(ServiceEnums.SMS_PHONE_NULL.getCode(),
                    RollConstants.ERROR,ServiceEnums.SMS_PHONE_NULL.getMessage());
        }
        if (StringUtils.isEmpty(code)) {
            return RollResponse.result(ServiceEnums.SMS_CODE_NULL.getCode(),
                    RollConstants.ERROR,ServiceEnums.SMS_CODE_NULL.getMessage());
        }
        if (!phone.matches(RollConstants.SMS_REGEX_PHONE)) {
            return RollResponse.result(ServiceEnums.SMS_REGEX_PHONE.getCode(),
                    RollConstants.ERROR,ServiceEnums.SMS_REGEX_PHONE.getMessage());
        }
        if (!code.matches(RollConstants.SMS_REGEX_CODE)) {
            return RollResponse.result(ServiceEnums.SMS_REGEX_CODE.getCode(),
                    RollConstants.ERROR,ServiceEnums.SMS_REGEX_CODE.getMessage());
        }

        return smsService.checkCode(phone,code);

    }
}
